package com.example.chao.myphotosearch.adapters;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.example.chao.myphotosearch.R;
import com.example.chao.myphotosearch.view.fragments.JoyFragment;
import com.example.chao.myphotosearch.view.fragments.MainFragment;
import com.example.chao.myphotosearch.view.fragments.NetImgFragment;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chao on 2016/3/23.
 */
public class MainPagerAdapter extends FragmentStatePagerAdapter{
    private String[] titles ;
    private Map<Integer,Fragment> fragments;
    public MainPagerAdapter(Context context,FragmentManager fm) {
        super(fm);
        fragments = new HashMap<>();
        titles = context.getResources().getStringArray(R.array.tab);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment;
        switch (position){
            case 0:
                fragment = new MainFragment();
                break;
            case 1:
                fragment = new JoyFragment();
                break;
            default:
                fragment = new NetImgFragment();
                Bundle bundle = new Bundle();
                bundle.putString("title",titles[position]);
                fragment.setArguments(bundle);
                break;
        }
        fragments.put(position,fragment);
        return fragment;
    }

    @Override
    public int getCount() {
        return titles.length;
    }

//        @Override
//        public Object instantiateItem(ViewGroup container, int position) {
//            Fragment fragment = (Fragment) super.instantiateItem(container, position);
//            return fragment;
//        }


    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

    public Fragment getFragment(int position){
        return fragments.get(position);
    }
}
